package tr.helper.repository;

import cn.hutool.core.map.MapUtil;
import tr.helper.repository.annotation.DbFiled;
import tr.helper.repository.annotation.Table;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

/**
 * tr-service
 *
 * @author MrYe
 * @date 2022-01-14 17:32
 */
public class Support {

    public static Map<Object, Object> mysqlMapping = MapUtil.of(new String[][]{
        {"class java.lang.String", "varchar"},
        {"class java.lang.Long", "bigint"},
        {"class java.math.BigDecimal", "decimal(12,8)"},
        {"class java.lang.Integer", "int"},
        {"class java.util.Date", "datetime"},
        {"class java.lang.Boolean", "bit"}
    });

    public static String getTableName(Class<?> clazz) {
        String tableName = clazz.getSimpleName();
        final Table table = clazz.getAnnotation(Table.class);
        if (null != table && StringUtils.isNotBlank(table.value())) {
            tableName = table.value();
        }
        return tableName;
    }

    public static int getLength(DbFiled dbFiled) {
        return dbFiled == null ? 32 : dbFiled.length();
    }

    public static String getIsNull(DbFiled dbFiled) {
        return dbFiled == null ? "NULL" : dbFiled.isNull() ? "NULL" : "NOT NULL";
    }

}
